package com.lzk.auth.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lzk.auth.service.OrderService;
import com.lzk.common.result.Result;
import com.lzk.model.base.CustomPage;
import com.lzk.model.system.SysOrder;

import com.lzk.model.system.SysProduct;
import com.lzk.vo.system.OrderVO;
import com.lzk.vo.system.PlanVo;
import com.lzk.vo.system.StartPlanVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lzk
 * @since 2023-12-30
 */
@Api(tags = "订单管理")
@RestController
@RequestMapping("/admin/system/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @ApiOperation("订单条件分页查询")
    @GetMapping("{page}/{limit}")
    public Result index(@PathVariable Long page,
                        @PathVariable Long limit,
                        OrderVO orderVO) {
        Page<SysOrder> pageParam = new Page<>(page, limit);

        List<SysOrder> sysOrders = orderService.getAll(orderVO,page,limit);

        IPage<SysOrder> pageModel = orderService.page(pageParam);
        pageModel.setRecords(sysOrders);
//        System.out.println(sysOrders);
//        CustomPage<SysOrder> customPage = new CustomPage<>();
//        customPage.setRecords(sysOrders);
//        customPage.setTotal(pageParam.getTotal());
//        customPage.setSize(pageParam.getSize());
//        customPage.setCurrent(pageParam.getCurrent());

        return Result.ok(pageModel);
    }

    @ApiOperation(value = "根据id查订单")
    @GetMapping("get/{id}")
    public Result get(@PathVariable Long id) {
        SysOrder order = orderService.getById(id);
        return Result.ok(order);
    }


    @ApiOperation(value = "删除订单")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable Long id) {

        UpdateWrapper<SysOrder> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id); // 根据ID作为更新条件
        updateWrapper.set("is_deleted", 1);
        orderService.update(updateWrapper);

        return Result.ok();
    }


    @ApiOperation(value = "查找空闲设备")
    @GetMapping("free/{id}")
    public Result selectFreeDevice(@PathVariable Long id) {
        PlanVo planVo = orderService.deviceFree(id);

        return Result.ok(planVo);
    }
    @ApiOperation(value = "查找空闲设备")
    @PostMapping("startPlan")
    public Result startPlan(@RequestBody StartPlanVO startPlanVO) {
        orderService.startPlan(startPlanVO);

        return Result.ok();
    }

}

